import matplotlib.pyplot as plt
import numpy as np

# 准备数据
n = 5
x = np.linspace(-1, 0, n)

y11 = np.array([15.00, 14.95, 15.08, 15.15, 15.66])
y12 = np.array([15.43, 15.42, 15.62, 15.66])

y21 = np.array([79.61, 80.37, 79.99, 81.02, 81.03])
y22 = np.array([80.64, 81.22, 80.38, 81.03])

y31 = np.array([19.23, 19.22, 18.34, 18.57, 19.02])
y32 = np.array([18.78, 18.72, 18.65, 19.02])

y41 = np.array([19.23, 19.22, 18.34, 18.57, 19.02])
y42 = np.array([18.78, 18.72, 18.65, 19.02])


y51 = np.array([19.23, 19.22, 18.34, 18.57, 19.02])
y52 = np.array([18.78, 18.72, 18.65, 19.02])

# 创建图形和坐标轴对象
fig, axs = plt.subplots(1, 5, figsize=(20, 4))

# 绘制第一个图
axs[0].plot(x, y11, label='bottom->top', color='steelblue', linestyle='-')
axs[0].scatter(x, y11, color='steelblue', marker='D', s=30)
axs[0].set_xticks(x)
axs[0].set_xticklabels(['10', '20', '30', '40', '50'])
axs[0].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_0 = axs[0].twinx()
ax2_0.plot(x[1:], y12, label='top->bottom', color='chocolate', linestyle='-.')
ax2_0.scatter(x[1:], y12, color='chocolate', marker='o', s=30)
ax2_0.tick_params(axis='y', colors='black')

axs[0].set_ylabel('Test accuracy(%)', fontsize=18, fontweight='normal')
axs[0].set_title('Chest(3)', fontsize=18, fontweight='normal')
axs[0].grid(True, which='both', color='lightgray', alpha=0.8)

# 绘制第二个图
axs[1].plot(x, y21, label='top->bottom', color='steelblue', linestyle='-')
axs[1].scatter(x, y21, color='steelblue', marker='D', s=30)
axs[1].set_xticks(x)
axs[1].set_xticklabels(['10', '20', '30', '40', '50'])
axs[1].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_1 = axs[1].twinx()
ax2_1.plot(x[1:], y22, label='bottom->top', color='chocolate', linestyle='-.')
ax2_1.scatter(x[1:], y22, color='chocolate', marker='o', s=30)
ax2_1.tick_params(axis='y', colors='black')

axs[1].set_title('Colon(3)', fontsize=18, fontweight='normal')
axs[1].grid(True, which='both', color='lightgray', alpha=0.8)

# 绘制第三个图
axs[2].plot(x, y31, label='top->bottom', color='steelblue', linestyle='-')
axs[2].scatter(x, y31, color='steelblue', marker='D', s=30)
axs[2].set_xticks(x)
axs[2].set_xticklabels(['10', '20', '30', '40', '50'])
axs[2].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_2 = axs[2].twinx()
ax2_2.plot(x[1:], y32, label='bottom->top', color='chocolate', linestyle='-.')
ax2_2.scatter(x[1:], y32, color='chocolate', marker='o', s=30)
ax2_2.tick_params(axis='y', colors='black')
ax2_2.set_ylabel('Similarity', fontsize=18, fontweight='normal', color='black')


axs[2].set_title('Endo(3)', fontsize=18, fontweight='normal')
axs[2].grid(True, which='both', color='lightgray', alpha=0.8)

# 绘制第四个图
axs[3].plot(x, y41, label='top->bottom', color='steelblue', linestyle='-')
axs[3].scatter(x, y41, color='steelblue', marker='D', s=30)
axs[3].set_xticks(x)
axs[3].set_xticklabels(['10', '20', '30', '40', '50'])
axs[3].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_3 = axs[3].twinx()
ax2_3.plot(x[1:], y42, label='bottom->top', color='chocolate', linestyle='-.')
ax2_3.scatter(x[1:], y42, color='chocolate', marker='o', s=30)
ax2_3.tick_params(axis='y', colors='black')
ax2_3.set_ylabel('Similarity', fontsize=18, fontweight='normal', color='black')


axs[3].set_title('ISIC(3)', fontsize=18, fontweight='normal')
axs[3].grid(True, which='both', color='lightgray', alpha=0.8)


# 绘制第五个图
axs[4].plot(x, y51, label='top->bottom', color='steelblue', linestyle='-')
axs[4].scatter(x, y51, color='steelblue', marker='D', s=30)
axs[4].set_xticks(x)
axs[4].set_xticklabels(['10', '20', '30', '40', '50'])
axs[4].tick_params(axis='x', colors='black')

# 添加第二个Y轴
ax2_4 = axs[4].twinx()
ax2_4.plot(x[1:], y52, label='bottom->top', color='chocolate', linestyle='-.')
ax2_4.scatter(x[1:], y52, color='chocolate', marker='o', s=30)
ax2_4.tick_params(axis='y', colors='black')
ax2_4.set_ylabel('Similarity', fontsize=18, fontweight='normal', color='black')


axs[4].set_title('APTOS(3)', fontsize=18, fontweight='normal')
axs[4].grid(True, which='both', color='lightgray', alpha=0.8)

# 设置标签颜色
for i_axs in axs:
    xtick_labels = i_axs.get_xticklabels()
    for label in xtick_labels:
        label.set_color('black')

# 共用一个图例
legend_handles = []
legend_labels = ['Acc', 'Sim']

twins_axs = [ax2_0,ax2_1,ax2_2,ax2_3,ax2_4] 
for i in range(5):
    legend_handles.append((axs[i].get_lines()[0], axs[i].collections[0]))
    legend_handles.append((twins_axs[i].get_lines()[0], twins_axs[i].collections[0]))

fig.legend(handles=legend_handles, labels=legend_labels, fontsize=18, loc='upper right', ncol=4)

# 调整子图的间距
fig.tight_layout()

plt.subplots_adjust(top=0.75)

# 保存图形
plt.savefig('Trade_off/b_train_epochs.png')
